<?php
/**
 *PageStructure Model
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * PageStructure_Model
 * Provides data to manipulate pagestructure
 *@package model
 */
class PageStructure_Model extends Model{
    /**
     * Reference to database connection
     * @var object
     */
    protected $oDBInstance;

    /**
     * Array to store the structure of the pages in a nested form
     * Every page holds its subpages as an array
     * @var array
     */
    protected $aNestedPageStructure;

    /**
     * Array to store the structure of the pages in a non nested form
     * The subpages are stored in the indexes directly after their parent page
     * @var array
     */
    protected $aTransformedPageStructure = array();

    /**
     * Interger used to transform nested array to the non nested array
     * @var integer
     */
    protected $iCounter = 0;

    /**
     * Model constructor
     * Establishes connection to database
     */
    public function __construct(){
        //get connection to database
        $this->oDBInstance = Mysql_dbconnection::getInstance();
    }

    /**
     * Returns the titel in the language specifiedof the page with the
     * @param integer $iPage ID of page
     * @param integer $iLang ID of the language
     */
    public function getPageTitle($iPage, $iLang){
        $this->oDBInstance->_setDefaultTable(
                'bs_page_language');
        $this->oDBInstance->_setDefaultColumn('id');

        $sParentID = $this->oDBInstance->getSQLValueString($iPage,'int');
        $this->oDBInstance->performSelect('name', '', 'pid = 1 AND lid =1', '');
        $aResult = $this->oDBInstance->fetch('array');

        return $aResult[0]['name'];
    }
    /**
     * Gets the page structure for the navigation
     * @param language
     */
    public function getPageStructure($iLanguage){
        $this->aNestedPageStructure = $this->getNestedPageStructure('NULL', $iLanguage);
        $this->createTransformedPageStructure($this->aNestedPageStructure);
        return $this->aTransformedPageStructure;
    }
    /**
     * Gets the nested page structure in an multi dimensional array
     * @param integer parent ID
     * @param integer language
     */
    public function getNestedPageStructure($iParentID, $iLanguage){
        $aPageStructure = array();
        $aPageStructure = $this->getSubPages($iParentID, $iLanguage);

        if (!(empty($aPageStructure))) {
            for ($i =0; $i< sizeof($aPageStructure); $i++){
                $aPage = &$aPageStructure[$i];
                $aPage['subPages'] = $this->getNestedPageStructure($aPage['id'], $iLanguage);

            }
        }
        return $aPageStructure;

    }
    /**
     * Gets the subpages of the passed page
     * @param integer page ID
     * @param integer language
     */
    public function getSubPages($iPid, $iLanguage){
        $this->oDBInstance->_setDefaultTable(
            	"(SELECT id, name, position, visible, parent, level, type
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND cms_only = 0 AND type != 'category' AND lid = $iLanguage) as a");
        $this->oDBInstance->_setDefaultColumn('id');
        $sParentID = $this->oDBInstance->getSQLValueString($iPid,'string');
        if ($iPid == 'NULL'){
            $this->oDBInstance->performSelect('id, name, position, visible, parent, level, type', '', "a.parent IS NULL ORDER BY position");
        }else {
            $this->oDBInstance->performSelect('id, name, position, visible, parent, level, type', '', "a.parent = $sParentID ORDER BY position");
        }
        return $this->oDBInstance->fetch('array');
    }
/**
 * Transforms the multi dimensional from nested page structure into an one dimensional
 * @param array nested page structure 
 */
    function createTransformedPageStructure($aArray){
        foreach($aArray as $aElement){
            $aPage = array();
            foreach($aElement as $sKey=>$mValue){
                if(!is_array($aElement[$sKey])){
                    $aPage[$sKey]=$aElement[$sKey];
                }
            }

            $this->aTransformedPageStructure[$this->iCounter] = $aPage;
            $this->iCounter++;
            if(!empty($aElement['subPages'])){
                $this->createTransformedPageStructure($aElement['subPages']);
            }
        }
    }
    /**
     * Deletes a page
     * @param integer page ID
     */
    public function deletePage($iPageID){
        $iPageID = $this->oDBInstance->getSQLValueString($iPageID, 'int');
        $this->oDBInstance->performSelect('id','bs_page',"parent = $iPageID");
        $aSubPages = $this->oDBInstance->fetch('array');
        if (empty($aSubPages)){
            $this->oDBInstance->performDelete($iPageID, 'bs_page_language','pid');
            $this->oDBInstance->performDelete($iPageID, 'bs_page','id');
        }else{

            return "Bitte erst alle Unterseiten/Kategorien löschen!";
        }

    }
    /**
     * Updates the page structure if a new page was created or the structure was sorted
     * @param integer language $iLanguage
     * @param integer page ID $iPageID
     * @param integer parent ID $iParentID
     * @param integer position $iPosition
     * @param integer visible $iVisible
     * @param integer name of the page
     */
    public function updatePageStructure($iLanguage, $iPageID, $iParentID, $iPosition, $iVisible,  $sName=''){
        $iLanguage = $this->oDBInstance->getSQLValueString($iLanguage, 'int');
        $iPageID  = $this->oDBInstance->getSQLValueString($iPageID, 'int');

        $iPostition = $this->oDBInstance->getSQLValueString($iPosition, 'int');
        if ($sName != ''){
            $sName = $this->oDBInstance->getSQLValueString($sName, 'string');
        }
        $iVisible = $this->oDBInstance->getSQLValueString($iVisible, 'int');
        if ($iVisible != 0){
            $iVisible = 1;
        }

        $iLevel = $iPageLevel = $this->getPageLevel($iParentID);
        //Updates page structure in the database bs_page
        $this->oDBInstance->query("UPDATE bs_page SET parent = $iParentID, position = '$iPosition', level = $iLevel, visible = '$iVisible' WHERE id = '$iPageID'");

        if($sName != ''){
            $this->oDBInstance->query("UPDATE bs_page_language SET name = $sName WHERE pid = '$iPageID' AND lid = '$iLanguage'");
        }

    }
    /**
     * Gets the page level which is necessary for navigation (main page or subpage)
     * @param integer page parent
     */
    protected function getPageLevel($iPageParent){
        $this->oDBInstance->_setDefaultTable('bs_page');
        $this->oDBInstance->_setDefaultColumn('id');
        if($iPageParent != 'NULL'){
            $this->oDBInstance->performSelect('level', '','','',$iPageParent);
            $aResult = $this->oDBInstance->fetch('array');
            return $aResult[0]['level']+1;
        }else{
            return 0;
        }
    }
    /**
     * Updates page's content in the database
     * @param integer page ID
     * @param string name of the page
     * @param string page content
     * @param integer language
     */
    public function updateContentPage($iPid, $sPageName, $sPageContent, $iLid){
        $iPid = $this->oDBInstance->getSQLValueString($iPid, 'int');
        $sPageName = $this->oDBInstance->getSQLValueString($sPageName, 'string');

        $this->oDBInstance->query("UPDATE bs_page_language SET name = $sPageName, content ='$sPageContent' WHERE lid='$iLid' AND pid = '$iPid'");

    }
    /**
     * Checks if the page is an content page or a product category
     * @param integer page id
     */
    public function isCategory($iPid){
        $iProductID = $this->oDBInstance->getSQLValueString($iPid, 'int');
        $this->oDBInstance->query("SELECT type FROM bs_page WHERE id = '$iPid'");

        $aResult = $this->oDBInstance->fetch('array');
        if(isset($aResult[0]['type'])){
            if($aResult[0]['type'] == 'category'){
                return TRUE;
            }
        }
        return FALSE;
    }


}
?>
